package com.inspur.jzsxxzx.controller.common;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.inspur.jzsxxzx.entity.muluzhili.UpDataCatalog;
import com.inspur.jzsxxzx.service.catalog.MuluzhiliVOService;
import com.inspur.jzsxxzx.service.muluzhili.UpDataCatalogService;
import com.inspur.jzsxxzx.vo.MuluzhiliVO;

@Controller
@RequestMapping("/fragments")
public class FragmentsController {

    @Autowired
    private MuluzhiliVOService muluzhiliVOService;

    @Autowired
    private UpDataCatalogService upDataCatalogService;

    @GetMapping("/catalog-relation")
    public String catalogRelationFragment(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String cataTitle,
            @RequestParam(required = false) String internalOrgName,
            Model model) {

        try {
            MuluzhiliVO condition = new MuluzhiliVO();
            if (cataTitle != null && !cataTitle.isEmpty()) {
                condition.setCataTitle(cataTitle);
            }
            if (internalOrgName != null && !internalOrgName.isEmpty()) {
                condition.setInternalOrgName(internalOrgName);
            }

            int totalCount = muluzhiliVOService.countByCondition(condition);
            int totalPages = (int) Math.ceil((double) totalCount / limit);

            List<MuluzhiliVO> pageData = muluzhiliVOService.findByPage(condition, page, limit);
            // 获取部门列表
            List<Map<String, String>> deptList = muluzhiliVOService.getAllDeptCodesAndNames();

            model.addAttribute("muluzhiliList", pageData);
            model.addAttribute("currentPage", page);
            model.addAttribute("totalPages", totalPages);
            model.addAttribute("totalCount", totalCount);
            model.addAttribute("cataTitle", cataTitle);
            model.addAttribute("internalOrgName", internalOrgName);
            model.addAttribute("deptList", deptList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "fragments/catalog-relation :: catalogRelation";
    }

    @GetMapping("/up-data-catalog")
    public String upDataCatalogFragment(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String cataTitle,
            @RequestParam(required = false) String importedByOrgName,
            Model model) {
        // 获取数据总数
        int totalCount = upDataCatalogService.countByCondition(cataTitle, importedByOrgName);
        // 获取当前页数据
        List<UpDataCatalog> pageData = upDataCatalogService.findPageByCondition(cataTitle, importedByOrgName, page,
                limit);

        // 将数据添加到模型中
        model.addAttribute("upDataCatalogList", pageData);
        model.addAttribute("currentPage", page);
        model.addAttribute("totalPages", (int) Math.ceil((double) totalCount / limit));
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("cataTitle", cataTitle);
        model.addAttribute("importedByOrgName", importedByOrgName);

        return "fragments/up-data-catalog :: upDataCatalog";
    }

    @GetMapping("/change-password")
    public String changePasswordFragment(Model model) {

        return "fragments/change-password :: changePassword";
    }
}
